package net.wapproject.action;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.wapproject.bean.Category;
import net.wapproject.utils.ConfigurationUtils;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;


/**
 * 
 * @author HoangAnh
 *
 */
public class BaseAction extends ActionSupport implements ServletContextAware {

private static final long serialVersionUID = -1050022320449630709L;
	
	private String action;
	protected ServletContext servletContext;
	private String fileUrl;
	private List<Category> categoryList;
	private File fileUpload;
	private String fileUploadContentType;
	private String fileUploadFileName;
	protected Integer pageNumber;
	protected Integer pageIndex;
	

	/**
	 * Check is given action or not.
	 * 
	 * @param action	action name
	 * @return			<code>true</code> or <code>false</code>
	 */
	public boolean isAction(String action) {
		
		return action.equalsIgnoreCase(this.action);
	}
	
	/**
	 * Convenience method to get the request.
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response.
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session. This will create a session if one
	 * doesn't exist.
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	/* ****************************** GETTERS & SETTERS ************************** */
	
	/**
	 * @return the action
	 */
	public String getAction() {
		return action;
	}
	/**
	 * @param action the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}
	/**
	 * Get context path. e.g http://localhost:8080/ezShop/
	 * 
	 * @return	context path.
	 */
	public String getContextPath() {
		return getRequest().getContextPath();
	}
	/**
	 * Set servlet context when the action is loaded.
	 * 
	 * @param servletContext	java servlet context
	 */
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	/**
	 * @return the fileUrl
	 */
	public String getFileUrl() {
		System.out.println(ConfigurationUtils.get(ConfigurationUtils.FILE_URL));
		return ConfigurationUtils.get(ConfigurationUtils.FILE_URL);
	}

	/**
	 * @param fileUrl the fileUrl to set
	 */
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
	
	
	public String getFileUploadContentType() {
		return fileUploadContentType;
	}
 
	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}
 
	public String getFileUploadFileName() {
		return fileUploadFileName;
	}
 
	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}
 
	public File getFileUpload() {
		return fileUpload;
	}
 
	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}
	public String upload() throws Exception{
		 
		return SUCCESS;
 
	}

	public String display() {
		return NONE;
	}

	/**
	 * @return the pageNumber
	 */
	public Integer getPageNumber() {
		return pageNumber;
	}

	/**
	 * @param pageNumber the pageNumber to set
	 */
	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	/**
	 * @return the pageIndex
	 */
	public Integer getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}
	
}
